<?php

namespace Home\Controller;

use Think\Controller;
use Think\Log;

class LihuaAppController extends Controller
{
    protected $conn;
    protected $connBj;
    protected $logFile   = '';
    protected $domain    = '';
    protected $domains   = array();
    protected $cityIds   = array();//对应新APP城市id
    protected $orderApi  = 'https://newapp.lihua.com/api/lhorder/lists';
    protected $statusApi = 'https://newapp.lihua.com/api/lhorder/updatestatus';

    public function _initialize()
    {
        $this->conn    = C('RMS_CONNECTSTR');
        $this->connBj  = C('BJRMS_CONNECTSTR');
        $this->domains = array(
            1 => 'bj.lihuaerp.com', 2 => 'sh.lihuaerp.com', 3 => 'cz.lihuaerp.com', 4 => 'nj.lihuaerp.com',
            5 => 'sz.lihuaerp.com', 6 => 'wx.lihuaerp.com', 7 => 'gz.lihuaerp.com',
        );
        $this->cityIds = array('bj' => 1, 'sh' => 2, 'cz' => 3, 'nj' => 4, 'sz' => 5, 'wx' => 6, 'gz' => 7);
    }

    // 获取北京的订单
    public function getBjWebOrder()
    {
        $this->getOrder('bj');
    }

    // 获取上海的订单
    public function getShWebOrder()
    {
        $this->getOrder('sh');
    }

    // 获取常州的订单
    public function getCzWebOrder()
    {
        $this->getOrder('cz');
    }

    // 获取南京的订单
    public function getNjWebOrder()
    {
        $this->getOrder('nj');
    }

    // 获取苏州的订单
    public function getSzWebOrder()
    {
        $this->getOrder('sz');
    }

    // 获取无锡的订单
    public function getWxWebOrder()
    {
        $this->getOrder('wx');
    }

    // 获取广州的订单
    public function getGzWebOrder()
    {
        $this->getOrder('gz');
    }

    // 获取订单/Home/LihuaApp/getOrder/city/cz
    public function getOrder($city = 'cz')
    {
        $this->logFile = C('LOG_PATH') . 'newGetOrder-'. $city .'-'. date('y_m_d') . '.log';
        if (isset($this->cityIds[$city])) {

            $res = $this->httpReq($this->orderApi, array('city_id' => $this->cityIds[$city]));
            print($res);
            $res = json_decode($res, true);
            print($res);
            if (json_last_error()) {
                $msg = "订单数据JSON解析失败";
                $this->addLog($msg); die($msg);
            }
            if ($res['code'] == 0) {
                $this->addLog($res['msg']); die($res['msg']);
            }

            $data = $res['data'];// 订单数据
            if ($data['orderList']) {
                $conn = $city == 'bj' ? $this->connBj : $this->conn;
                foreach ($data['orderList'] as $k => $v) {
                    $goodsList = isset($data['goodsList'][$v['id']]) ? $data['goodsList'][$v['id']] : null;
                    $payList   = isset($data['payList'][$v['id']]) ? $data['payList'][$v['id']] : null;
                    $actList = isset($data['actList'][$v['sn']]) ? $data['actList'][$v['sn']] : null;
                    // 订单数据写入RMS
                    $addRes = $this->add2rms($v, $goodsList, $payList, $actList, $conn);
                    if (!$addRes) continue;
                    // 更新来源订单状态为已确认
                    $res = $this->httpReq($this->statusApi,
                        array(
                            'sn'     => $v['sn'],//订单号
                            'status' => 3,//状态类型
                            'des'    => '订单确认',//配送状态描述
                        )
                    );
                    $res = json_decode($res, true);
                    if ($res['code'] == 0) {
                        $this->addLog("修改订单{$v['sn']}状态为 订单确认 失败：{$res['msg']}");
                    }
                }
            }
        } else {
            $this->addLog("{$city}城市不存在");
            die("{$city}城市不存在");
        }
    }


    // 将订单写入RMS
    protected function add2rms($row, $goodsList, $payList, $actList, $conn)
    {
        $orderModel      = M('orderform', 'rms_', $conn);//订单表
        $goodsModel      = M('orderproducts', 'rms_', $conn);//订单商品表
        $payModel        = M('orderpayment', 'rms_', $conn);//支付表
        $financeModel    = M('orderfinance', 'rms_', $conn);//结账单
        $stateModel      = M('orderstate', 'rms_', $conn);//状态表
        $actionModel     = M('orderaction', 'rms_', $conn);//日志表
        $yingshouModel   = M('orderyingshouexchange', 'rms_', $conn);//营收状态表
        $webstatusModel  = M('webstatus', 'rms_', $conn);//网站状态表
        $invoiceModel    = M('invoice', 'rms_', $conn);//发票表
        $notifyModel     = M('notify_client', 'rms_', $conn);//客户消息

        // 写订单表
        $this->domain = $domain = $this->domains[$row['city_id']];//配送地区标识
        list($custdate, $custtime) = explode(' ', $row['deliver_time']);
		
        //春节期间门店调整
		// $value=='金桥' || $value=='张江' || $value=='陆家嘴' || $value=='宝山' || $value=='杨浦'
        //北京地区

       // 通州、奥体、中关村、丰台、国贸、朝阳门、石景山、亦庄、工体
       // 通州，菜户营 ，工体，望京，中关村 亚运村、望京
       // 国贸 北科大 奥体 酒仙桥 石景山 光彩 西二环 丰台
       // 亦庄 东五环 永定门 工体 望京 亚运村 北三环 西二环 西四环

       //开发区 国贸 酒仙桥 奥体 首体 西客站 丰台 光彩 石景山


        // 北京地区 亦庄店 、东五环店 、永定门店、工体店、望京店、亚运村店、北三环店、西二环店、西四环店
        // 开发区 国贸  酒仙桥  奥体  首体  西客站  丰台  光彩  石景山   

        if($row['store_name'] == '农展店'){  $row['store_name']  =  '工体';  }
        // if($row['store_name'] == '亚运村'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '亚运村店'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '中关村'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '中关村店'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '开发区'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '北工大'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '朝阳门'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '奥体'){  $row['store_name']  =  '';  }
        if($row['store_name'] == '月坛'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '石景山'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '菜户营'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '北科大'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '光彩'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '工体'){  $row['store_name']  =  '';  } //工体 王府井 北科大 酒仙桥 丰台 开发区
        // if($row['store_name'] == '首体'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '通州店'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '丰台'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '国贸'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '亦庄'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '亦庄店'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '东五环'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '永定门'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '望京'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '北三环'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '西二环'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '西客站'){  $row['store_name']  =  '';  }
        // if($row['store_name'] == '西四环'){  $row['store_name']  =  '';  }
	    // if($row['store_name'] == '王府井'){  $row['store_name']  =  '';  }
	    // if($row['store_name'] == '酒仙桥'){  $row['store_name']  =  '';  }
		//上海地区
        if($row['store_name'] == '金桥'){  $row['store_name']  = '';  }
        if($row['store_name'] == '张江'){  $row['store_name']  = '';  }
        if($row['store_name'] == '陆家嘴'){  $row['store_name']  = '';  }
        if($row['store_name'] == '宝山'){  $row['store_name']  = '';  }
        if($row['store_name'] == '杨浦'){  $row['store_name']  = '';  }
		//无锡地区
		if($row['store_name'] == '金泰'){  $row['store_name']  = '';  }
		if($row['store_name'] == '太湖'){  $row['store_name']  = '';  }
		if($row['store_name'] == '凤翔'){  $row['store_name']  = '';  }
		if($row['store_name'] == '新区'){  $row['store_name']  = '';  }
		if($row['store_name'] == '人民路'){  $row['store_name']  = '';  }
		if($row['store_name'] == '清扬'){  $row['store_name']  = '';  }
		if($row['store_name'] == '五爱'){  $row['store_name']  = '';  }
        //苏州地区
        // if($row['store_name'] == '圆融'){  $row['store_name']  = '';  }
		// if($row['store_name'] == '胜浦'){  $row['store_name']  = '';  }
		//南京地区
		if($row['store_name'] == '白下路'){  $row['store_name']  = '';  }
		if($row['store_name'] == '财大'){  $row['store_name']  = '';  }
		if($row['store_name'] == '徐庄'){  $row['store_name']  = '';  }
		if($row['store_name'] == '云龙山路'){  $row['store_name']  = '';  }
		if($row['store_name'] == '黄山路'){  $row['store_name']  = '';  }
		if($row['store_name'] == '迈皋桥'){  $row['store_name']  = '';  }
        
		//上海地区
		if($row['store_name'] == '延安'){  $row['store_name']  = '';  }
		if($row['store_name'] == '张江'){  $row['store_name']  = '';  }
		if($row['store_name'] == '合川'){  $row['store_name']  = '';  }
		if($row['store_name'] == '真北'){  $row['store_name']  = '';  }
		if($row['store_name'] == '虹口'){  $row['store_name']  = '';  }
		if($row['store_name'] == '陆家嘴'){  $row['store_name']  = '';  }
		if($row['store_name'] == '外滩'){  $row['store_name']  = '';  }
		if($row['store_name'] == '宝山'){  $row['store_name']  = '';  }
		if($row['store_name'] == '国展'){  $row['store_name']  = '';  }
		if($row['store_name'] == '杨浦'){  $row['store_name']  = '';  }
		if($row['store_name'] == '徐汇'){  $row['store_name']  = '';  }
		if($row['store_name'] == '普陀'){  $row['store_name']  = '';  }
		if($row['store_name'] == '浦东'){  $row['store_name']  = '';  }		 
		if($row['store_name'] == '闵行'){  $row['store_name']  = '';  }		 
		if($row['store_name'] == '江场'){  $row['store_name']  = '';  }		 
		if($row['store_name'] == '金桥'){  $row['store_name']  = '';  }		 
		if($row['store_name'] == '黄浦'){  $row['store_name']  = '';  }
        // 通州 、 奥体、 中关村 、 丰台 、 朝阳门 、 石景山 、、 工体 
		// if(strpos($row['remark'],'无接触配送') === false){
		// 	$row['remark'] = $row['remark'] . '[无接触配送]';
        // }
        
        // if ($row['wallet_given_fee'] > 0) {//钱包赠送金额支付
		//     $row['original_amount'] = bcsub($row['original_amount'], $row['wallet_given_fee'], 2 );
        // }

        // if ($row['wallet_random_fee'] > 0) {//钱包支付随机立减金额
		//     $row['original_amount'] = bcsub($row['original_amount'], $row['wallet_random_fee'], 2 );
        // }
    
        //上海地区关闭全所有门店的自动分配
        if((int)$row['city_id']==2){
            $row['store_name']  = '';
        }

        //常州地区满300不自动分配 4月11号之前赠送精美点心礼盒一份
        if((int)$row['original_amount']>=300 && (int)$row['city_id']==3){
            $row['store_name']  = '';
            if(time()<strtotime("2021-04-11 23:59:59")){
                $row['remark'] = '【赠送精美点心礼盒一份】'.$row['remark'];
            }
        }
		
        $data = array(
            'ordersn'          => $row['sn'],//订单号
            'clientname'       => $row['consignee'],//联系人姓名
            'address'          => $row['address'],//送餐地址
            'telphone'         => $row['mobile'],//收货人手机
            'custdate'         => $custdate,//送餐日期
            'custtime'         => $custtime,//送餐时间
            'telname'          => $row['platform'],//接单员
            'rectime'          => date('H:i:s'),//接单时间
            'recdate'          => date('Y-m-d'),//接单日期
            'beizhu'           => $row['remark'],//订单备注
            'totalmoney'       => $row['original_amount'],//订单总金额
            'shouldmoney'      => $row['pay_status'] == 0 ? $row['order_amount'] : 0,//应收金额
            'paidmoney'        => $row['pay_status'] == 1 ? $row['order_amount'] : 0,//已付金额
            'invoiceheader'    => $row['invo_title'],//发票抬头
            'invoicebody'      => $row['invo_title'] ? '工作餐' : '',//发票内容
            'gmf_nsrsbh'       => $row['invo_taxno'],//纳税人识别号
            'gmf_dzdh'         => '',//购买方地址电话
            'gmf_yhzh'         => '',//购买方银行账户
            'invoicetype'      => $row['invo_title'] ? '电子票' : '',
            'shippingname'     => '自配送',//送餐费名称
            // 'shippingmoney'    => $row['deliver_fee'],//送餐费金额
            'company'          => $row['store_name'],//分公司门店   $row['store_name']
            'ordertxt'         => '',//商品简述
            'longitude'        => $row['lng'],//订单坐标经度
            'latitude'         => $row['lat'],//订单坐标维度
            'ap'               => $custtime >= '15:00:00' ? '下午' : '上午',//午别
            'state'            => '订餐',
            'origin'           => $row['platform'],//订单来源
            'domain'           => $domain,//配送地区标识
        );
        
        try {
            $haveorder = $orderModel->where(array('ordersn'=>$row['sn']))->find();
            if($haveorder){
                return false;
            }else{
                $orderformid = $orderModel->add($data);
            }
        } catch (\Exception $e) {
            $this->addLog('订单' . $row['sn'] . '写入失败：' . $orderModel->getLastSql());
            return false;
        }

        // 写订单商品表
        $boxArr     = array();
        $data       = array();
        $orderTxt   = array();
        $goodsMoney = $row['goods_amount'];//商品总金额
        if ($goodsList) {
            foreach ($goodsList as $k => $v) {
                $shortName = $v['short_name'] ?: $v['name'];
                $data[] = array(
                    'orderformid'  => $orderformid,//订单id
                    'ordersn'      => $row['sn'],//订单号
                    'code'         => '',//商品code
                    'name'         => $v['name'],//商品名称
                    'shortname'    => $shortName,//商品简称
                    'price'        => $v['price'],//商品单价
                    'number'       => $v['num'],//商品数量
                    'money'        => $v['num_price'],//商品金额
                    'domain'       => $domain,//配送地区标识
                );
                $orderTxt[] = $v['num'] . '×' . $shortName;//商品简述

                $boxFee = round($v['box_fee'], 2);//餐盒费
                if ($boxFee > 0) $boxArr["$boxFee"][] = $v['num'];
            }

            // 餐盒费写入商品表
            if (!empty($boxArr)) {
                foreach ($boxArr as $k => $v) {
                    $num  = array_sum($v);
                    $name = $k . '元餐盒费';
                    $data[] = array(
                        'orderformid'  => $orderformid,
                        'ordersn'      => $row['sn'],
                        'code'         => '',
                        'name'         => $name,
                        'shortname'    => $name,
                        'price'        => $k,
                        'number'       => $num,
                        'money'        => $k * $num,
                        'domain'       => $domain,
                    );
                    $orderTxt[] = $num . '×' . $name;
                    $goodsMoney += $k * $num;
                }
            }

            // 送餐费写入商品表
            if ($row['deliver_fee'] > 0) {
                $data[] = array(
                    'orderformid'  => $orderformid,
                    'ordersn'      => $row['sn'],
                    'code'         => '',
                    'name'         => round($row['deliver_fee'], 2) . 'S',
                    'shortname'    => round($row['deliver_fee'], 2) . 'S',
                    'price'        => $row['deliver_fee'],
                    'number'       => 1,
                    'money'        => $row['deliver_fee'],
                    'domain'       => $domain,
                );
                $orderTxt[]  = '1×' . round($row['deliver_fee'], 2) . 'S';
                $goodsMoney += $row['deliver_fee'];
            }
            if ($data) $goodsModel->addAll($data);
        }

        // var_dump($row);
        
        // var_dump($row['city_packing_fee'] > 0);

        // 写活动表
        if ($row['enough_fee'] > 0) {//满减
            $this->activity($orderformid, $row['sn'], $row['enough_name'], $row['enough_fee'], $conn);
        }
        if ($row['bonus_fee'] > 0) {//红包
            $this->activity($orderformid, $row['sn'], $row['bonus_name'], $row['bonus_fee'], $conn);
        }
		if ($row['wallet_given_fee'] > 0) {//钱包赠送金额支付
		    $this->activity($orderformid, $row['sn'], '钱包赠送金额支付', $row['wallet_given_fee'], $conn);
        }
        if ($row['wallet_random_fee'] > 0) {//钱包支付随机立减金额
		    $this->activity($orderformid, $row['sn'], '钱包支付随机立减', $row['wallet_random_fee'], $conn);
        }
        if ($row['wallet_packing_fee'] > 0) {//钱包支付免餐盒费
		    $this->activity($orderformid, $row['sn'], '钱包支付免餐盒费', $row['wallet_packing_fee'], $conn);
        }
        if ($row['vouchers_fee'] > 0) {//使用现金抵扣券
		    $this->activity($orderformid, $row['sn'], '使用现金抵扣券', $row['vouchers_fee'], $conn);
        }
        // if ($row['city_packing_fee'] > 0) {//满额免餐盒费 city_packing_fee
		//     $this->activity($orderformid, $row['sn'], '满额免餐盒费', $row['city_packing_fee'], $conn);
        // }

        $activity_money = 0;
        if ($actList){
            foreach ($actList as $k => $v) {
                $this->activity($orderformid, $v['order_sn'], $v['name'], $v['discount'], $conn);
                $activity_money += $v['discount'];
            }
        }

        // 写支付表
        if ($payList) {
            foreach ($payList as $k => $v) {

                // if ($row['wallet_given_fee'] > 0) {//钱包赠送金额支付
                //     $v['pay_money'] = bcsub($v['pay_money'], $row['wallet_given_fee'], 2 );
                // }
        
                // if ($row['wallet_random_fee'] > 0) {//钱包支付随机立减金额
                //     $v['pay_money'] = bcsub($v['pay_money'], $row['wallet_random_fee'], 2 );
                // }

                $payModel->add(
                    array(
                        'orderformid' => $orderformid,//订单ID
                        'ordersn'     => $row['sn'],//订单号
                        'paymentid'   => 0,//支付ID
                        'name'        => $v['pay_name'],//支付名称
                        'money'       => $v['pay_money'],//支付金额
                        'note'        => '',//支付备注
                    )
                );
                // 结账修正，改为orderfinance
                // 插入结账的数据
                $financeModel->add(
                    array(
                        'ordersn'     => $row['sn'],
                        'name'        => $v['pay_name'],
                        'money'       => $v['pay_money'],
                        'date'        => date('Y-m-d H:i:s'),
                        'domain'      => $domain,
                    )
                );
            }
        }

        // 更新订单信息
        $orderTxt = join(' ', $orderTxt);
        $data = array(
            'ordertxt'   => $orderTxt,
            'goodsmoney' => $goodsMoney,
        );
        $orderModel->where(array('orderformid' => $orderformid))->save($data);


        // 写状态表
        $stateModel->add(
            array(
                'create'         => 1,
                'createtime'     => date('Y-m-d H:i:s'),
                'createcontent'  => '网站输入',
                'orderformid'    => $orderformid,
                'ordersn'        => $row['sn'],
                'domain'         => $domain,
            )
        );

        // 写操作到action中
        $actionModel->add(
            array(
                'orderformid'    => $orderformid,
                'ordersn'        => $row['sn'],
                'action'         => '网站下载 ' . $row['address'] . ' ' . $orderTxt . ' ' . $row['remark'],
                'logtime'        => date('H:i:s'),
                'domain'         => $domain,
            )
        );

        // 写操作到action中
        if(!!$row['store_name']){
            $actionModel->add(
                array(
                    'orderformid'    => $orderformid,
                    'ordersn'        => $row['sn'],
                    'action'         => '订单已分配到【' . $row['store_name'] . '】',
                    'logtime'        => date('H:i:s'),
                    'domain'         => $domain,
                )
            );
        }

        // 写营收状态表，驱动营收结账
        $yingshouModel->add(
            array(
                'orderformid'    => $orderformid,
                'ordersn'        => $row['sn'],
                'status'         => 0,
                'assisstatus'    => 1,
                'domain'         => $domain,
            )
        );

        // 写网站状态接口表
        $webstatusModel->add(
            array(
                'ordersn'        => $row['sn'],
                'type'           => 1,//表示派单
                'content'        => '订单确认',
                'date'           => date('Y-m-d H:i:s'),
                'origin'         => $row['platform'],
                'domain'         => $domain,
            )
        );

        // 通知客户的消息
        $notifyModel->add(
            array(
                'ordersn'        => $row['sn'],
                'telphone'       => '',
                'app_tk'         => '',
                'contenttype'    => 'confirm',
                'origin'         => $row['platform'],
                'domain'         => $domain,
            )
        );

        // 保存发票
        if ($row['invo_title']) {
            $invoiceModel->add(
                array(
                    'header'       => $row['invo_title'],
                    'body'         => '工作餐',
                    'gmf_nsrsbh'   => $row['invo_taxno'],
                    'gmf_dzdh'     => '',
                    'gmf_yhzh'     => '',
                    'type'         => 3,// 2普通发票，3电子发票
                    'ordersn'      => $row['sn'],
                    'orderformtxt' => $row['address'] . ' ' . $orderTxt,
                    'ordermoney'   => $row['order_amount'], 
                    'ordertime'    => date('H:i:s'),
                    'state'        => '未开',
                    'company'      => $row['store_name'],
                    'domain'       => $domain,
                )
            );
        }

        return true;
    }

    // 活动表及结账
    public function activity($orderFormId, $orderSn, $activityName, $activityMoney, $conn)
    {
        M('orderactivity', 'rms_', $conn)->add(
            array(
                'orderformid'  => $orderFormId,
                'ordersn'      => $orderSn,
                'activityid'   => 0,
                'name'         => $activityName,
                'money'        => $activityMoney,
                'note'         => '',
            )
        );
        // 为了结账的需要，活动费
        M('orderfinance', 'rms_', $conn)->add(
            array(
                'ordersn'      => $orderSn,
                'name'         => '官网活动费',
                'money'        => $activityMoney,
                'date'         => date('Y-m-d H:i:s'),
                'domain'       => $this->domain,//原本都存的是空值
            )
        );
    }


    // 获取rms中webstate, 写入网站中
    public function setBjWebStatus()
    {
        $this->setStatus('bj');
    }

    public function setShWebStatus()
    {
        $this->setStatus('sh');
    }

    public function setCzWebStatus()
    {
        $this->setStatus('cz');
    }

    public function setNjWebStatus()
    {
        $this->setStatus('nj');
    }

    public function setSzWebStatus()
    {
        $this->setStatus('sz');
    }

    public function setWxWebStatus()
    {
        $this->setStatus('wx');
    }

    public function setGzWebStatus()
    {
        $this->setStatus('gz');
    }

    // 设置状态 /Home/LihuaApp/setStatus/city/cz
    public function setStatus($city = 'cz')
    {
        $this->logFile = C('LOG_PATH') . 'newSetStatus-'. $city .'-'. date('y_m_d') . '.log';
        if (isset($this->cityIds[$city])) {
            $city_id = $this->cityIds[$city];
            $domain  = $this->domains[$city_id];// 配送地区标识
            $conn    = $city == 'bj' ? $this->connBj : $this->conn;
            $webstatusModel = M('webstatus', 'rms_', $conn);// 网站状态表

            //老系统 订单状态：0=客户下单,1=订单确认,2=分配到配送点,3=派单给送餐员,4=订单配送完成,8=下单失败,10=逻辑删除
            //新系统 订单状态：0=已取消,1=待支付,2=待确认,3=待分配,4=待派送,5=派送中,6=已完成
            $newStatus = array(0 => 2, 1 => 3, 2 => 4, 3 => 5, 4 => 6, 8 => 0, 10 => 0);

            $where = "`domain`='{$domain}' AND `state`=0 AND (`origin`='app' OR `origin`='xcx')";
			// var_dump($where);
            $list  = $webstatusModel->where($where)->limit(1000)->select();
			var_dump($webstatusModel->getLastSql());
			//var_dump($list);
            if ($list) {
                foreach ($list as $k => $v) {
                    if (isset($newStatus[$v['type']])) {
                        // 更新来源订单状态
                        $res = $this->httpReq($this->statusApi,
                            array(
                                'sn'     => $v['ordersn'],//订单号
                                'status' => $newStatus[$v['type']],//状态类型
                                'des'    => $v['content'],//配送状态描述
                            )
                        );
						var_dump(array(
                                'sn'     => $v['ordersn'],//订单号
                                'status' => $newStatus[$v['type']],//状态类型
                                'des'    => $v['content'],//配送状态描述
                            ));
						// var_dump($res);
                        $res = json_decode($res, true);
                        var_dump($res);
                        if ($res['code'] == 0) {
                            $this->addLog("修改订单{$v['ordersn']}状态失败：{$res['msg']}");
                        }else{
							$webstatusModel->where(
								array(
									'ordersn' => $v['ordersn'],
									'webstatus' => $v['webstatus'],
								)
							)->save(array('state' => 1));
						}
						$webstatusModel->where(
						    array(
						        'ordersn' => $v['ordersn'],
						        'webstatus' => $v['webstatus'],
						    )
						    )->save(array('state' => 1));
                    }
                }
            }
        } else {
            $this->addLog("{$city}城市不存在");
            die("{$city}城市不存在");
        }
    }

    // 写日志
    public function addLog($msg)
    {
        Log::write($msg, Log::INFO, '', $this->logFile);
    }

    // http请求
    public function httpReq($url, $data = null){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        if ($data){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array("Expect:"));
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    // 空方法
    public function _empty()
    {
        exit;
    }

}
